package com.zz.framework.core.utils.language

import android.text.TextUtils
import androidx.annotation.StringRes
import com.zz.framework.core.BaseApp
import com.zz.framework.core.ext.fromJson
import com.zz.framework.core.ext.getLangKey
import com.zz.framework.core.ext.printError
import com.zz.framework.core.ext.toJson
import com.zz.framework.core.ext.toPinyinLowerCase
import com.zz.framework.core.utils.DataStoreUtils
import com.zz.framework.core.utils.LogUtils


/**
 * Copyright © 2024 zhun All rights reserved.
 * Created by Zha0Zhun on 2024/3/11 09:02.
 * @author: Zha0Zhun 1820022519@qq.com
 * @version: V1.0
 */
object LanguageManager {
    private var languageMap: MutableMap<String, String> = mutableMapOf()
//    fun saveLanguageList(version: String?, language: String, list: List<LanguageEntity>) {
//        if (list.isNotEmpty()) {
//            languageMap.clear()
//            for ((_, langKey, langContext) in list) {
//                languageMap[langKey?.lowercase()] = langContext
//            }
//        }
//        DataStoreUtils.putSyncData("SP_LANGUAGE", language)
//        DataStoreUtils.putSyncData("SP_LANGUAGE_LIST_$language", languageMap.toJson())
//        DataStoreUtils.putSyncData("SP_LANGUAGE_VERSION_$language", version)
//    }

    var currentLangCode: String
        get() {
            val language: String = DataStoreUtils.getSyncData("SP_LANGUAGE", "zh-CN")
            return language
        }
        set(langageCode) {
            DataStoreUtils.putSyncData("SP_LANGUAGE", langageCode)
        }

    var currentLangValue: String
        get() {
            val language: String = DataStoreUtils.getSyncData("SP_LANGUAGE_VALUE", "简体中文")
            return language
        }
        set(langageValue) {
            DataStoreUtils.putSyncData("SP_LANGUAGE_VALUE", langageValue)
        }

    fun getVersion(language: String): String {
        val version: String = DataStoreUtils.getSyncData("SP_LANGUAGE_VERSION_$language", "0")
        return version
    }

    private fun getLanguageMap(): Map<String, String> {
        return getLanguageMap(currentLangCode)
    }

    fun resetLanguageMap(language: String = LanguageManager.currentLangCode) {
        languageMap = if (TextUtils.equals(language, "en")) Strings.enMap else Strings.zhMap
    }

    fun getLanguageMap(language: String): Map<String, String> {
        if (languageMap.isEmpty()) {
//            val languageStr: String = DataStoreUtils.getSyncData("SP_LANGUAGE_LIST_$language", "{}")
//            val languageMapTemp: MutableMap<String?, String?> = languageStr.fromJson<MutableMap<String?, String?>>()
//            if (languageMapTemp.isNotEmpty()) {
            languageMap = if (TextUtils.equals(language, "en")) Strings.enMap else Strings.zhMap
//            }
        }
        return languageMap
    }

    fun getString(name: String): String {
        val key = name.getLangKey()
        if (languageMap.isEmpty()) getLanguageMap()
        val value = languageMap.getOrDefault(key, name).orEmpty()
        LogUtils.info("key=$key   value:$value")
        return value
    }


    fun getString_(name: String): String {
        val key = name.getLangKey()
        if (languageMap.isEmpty()) getLanguageMap()
        val value = languageMap.getOrDefault(key, name).orEmpty() + ":"
        LogUtils.info("key=$key   value:$value")
        return value
    }

    fun getString(@StringRes resId: Int): String {
        val name: String = BaseApp.context.getString(resId)
        val key = name.getLangKey()
        if (languageMap.isEmpty()) getLanguageMap()
        val value = languageMap.getOrDefault(key, name).orEmpty()
        LogUtils.info("key=$key   value:$value")
        return value
    }

    fun getString_(@StringRes resId: Int): String {
        val name: String = BaseApp.context.getString(resId)
        val key = name.getLangKey()
        val value = languageMap.getOrDefault(key, name).orEmpty() + ":"
        LogUtils.info("key=$key   value:$value")
        return value
    }


    fun getString(@StringRes resId: Int, vararg formatArgs: Any?): String {
        val name: String = BaseApp.context.getString(resId)
        val key = name.getLangKey()
        if (languageMap.isEmpty()) getLanguageMap()
        val value = if (languageMap.containsKey(key)) {
            String.format(languageMap[key]!!, *formatArgs)
        } else {
            BaseApp.context.getString(resId, formatArgs)
        }
        LogUtils.info("key=$key   value:$value")
        return value
    }


    fun initLanguage(languageCode: String) {
        val fieldMap = mutableMapOf<String, String>()
        Strings::class.java.declaredFields.forEach { field ->
            field.isAccessible = true
            val key = field.name
            val pinyinKey = key.getLangKey()
            val value = field.get(Strings)
            if (value is String) {
                fieldMap[pinyinKey] = value
            }
        }
        fieldMap.toJson().printError()
    }
}
